package com.service.socket.process;

import java.util.Map;

import org.jboss.netty.channel.Channel;

import com.service.bean.device.Vehicle;
import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.socket.protocol.CmdWord;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;

public class R_Class_C0 extends R_Class_Abstract {

	public R_Class_C0(Channel channel, Packet packet) {
		super(channel, packet);
	}

	@Override
	public void execute() {
		try {
			int cmdword = Integer.parseInt(packet.cmdWord, 16);
			switch (cmdword) {
			case 0x00:
				method_00();
				break;
			case 0x01:
				method_01();
				break;
			case 0x02:
				method_02();
				break;
			case 0x03:
				break;
			case 0x04:
				method_04();
				break;
			case 0x05:
				break;
			case 0x06:
				break;
			default:
				break;
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}

	/**
	 * ctrl登录 
	 */
	public void method_00() {
		String[] strArr = Conversion.hexStringToGb2312(packet.dataArea).split(",");
		String user = strArr[0];
		String type = strArr[1];
		String userType = "00".equals(type)?"user ctrl ":"dtu ctrl ";
		Map<String,Channel> channelMap = "00".equals(type)?CacheManager.instance().ctrlUserChannel:CacheManager.instance().ctrlDtuChannel;
		if (channelMap.containsKey(user)) {
			channelMap.get(user).close();
			Logger.println(userType+"["+user+"] disconnect the old connection");
		}
		channelMap.put(user, channel);
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
		Logger.println(userType+"[" + user + "] regist successed");
	}

	/**
	 * ctrl断开
	 */
	private void method_01() {
		String[] strArr = Conversion.hexStringToGb2312(packet.dataArea).split(",");
		String user = strArr[0];
		String type = strArr[1];
		String userType = "00".equals(type)?"user ctrl ":"dtu ctrl ";
		Map<String,Channel> channelMap = "00".equals(type)?CacheManager.instance().ctrlUserChannel:CacheManager.instance().ctrlDtuChannel;
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
		channel.close();
		if (channelMap.containsKey(user)) {
			channelMap.remove(user);
		}
		Logger.println(userType+"["+user+"] shut down");
	}

	/**
	 * ctrl读取在线设备
	 */
	private void method_02() {
		String companyCode = Conversion.hexStringToGb2312(packet.dataArea);
		packet.status = CmdWord.STATUS_SUCCESS;
		StringBuilder builder = new StringBuilder();
		for(String x : CacheManager.instance().sessionMap.keySet())
		{
			Vehicle v = CacheManager.instance().getVehicle(x);
			if(v != null && v.company.code.equals(companyCode)){
				builder.append(x);
				builder.append(",");
			}
		}
		if(builder.length()>0){
			builder.deleteCharAt(builder.length()-1);
		}
		packet.dataArea = Conversion.gb2312ToHexString(builder.toString());
		SendCommand.replyCtrl(channel,packet);
	}

	private void method_04() {
		String[] strArr = Conversion.hexStringToGb2312(packet.dataArea).split(",");
		String dtuNo = strArr[0];
		String version = strArr[1] + ".bin";
		//String ctrlUser = packet.getDeviceNo();
		Packet bean = PacketCreator.getFrameBean(dtuNo, version);
		SendCommand.send(bean);
		System.out.println("server -> dtu : " + bean.toString());
		//SendCommand.addMoniter(dtuNo, ctrlUser, bean.cmdType);
		//packet.status = CmdWord.STATUS_SUCCESS;
		//packet.dataArea = null;
		SendCommand.replyCtrl(channel,bean);
	}
}
